#include "VertexArray.h"
#include "Renderer.h"


VertexArray::VertexArray() {
	GLCALL(glGenVertexArrays(1, &m_RenderID));
}

VertexArray::~VertexArray()
{
	GLCALL(glDeleteVertexArrays(1, &m_RenderID));
}

void VertexArray::Bind() const
{
	GLCALL(glBindVertexArray(m_RenderID));
}

void VertexArray::Unbind() const
{
	GLCALL(glBindVertexArray(0));
}

void VertexArray::AddBuffer(const VertexBuffer& vb, const VertexBufferLayout& layout)
{
	Bind();
	vb.Bind();
	const auto& elements = layout.m_Elements;
	int count = elements.size();
	int offset = 0;
	for (int i = 0; i < count; i++)
	{
		const auto& element = elements[i];
		GLCALL(glEnableVertexAttribArray(i));
		GLCALL(glVertexAttribPointer(i, element.size, element.type,element.normalized,layout.GetStride(), (const void*)offset));
		offset += element.size * sizeof(element.type);
	}
}
